#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using System.Web;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlTicketPurchase : UserControl
    {
        User m_current_user;
        TicketPurchaseService m_ticketPurchaseService;
        TravelAgentService m_TravelAgentService;
        Keys m_currentKey;
        MainForm m_mainForm;
        //EditMode m_editmode;

        public UserControlTicketPurchase(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            m_current_user = currentUser;
            m_ticketPurchaseService = new TicketPurchaseService();// (TicketPurchaseService)ServiceFactory.Instance().GetService(ServiceFactory.TICKET_PURCHASE_SERVICE);
            m_TravelAgentService = new TravelAgentService();// (TravelAgentService)ServiceFactory.Instance().GetService(ServiceFactory.TRAVEL_AGENT_SERVICE);
            m_mainForm = mainForm;
            setPermission();
            textBoxSearch.SelectAll();
            LoadAllTicketPurchase();
            initToolBarButtonClick();
            enableButton(false);
        }


        private void initToolBarButtonClick()
        {
            toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            toolBarButtonView.Click += new EventHandler(toolBarButtonView_Click);
            toolBarButtonPrint.Click += new EventHandler(toolBarButtonPrint_Click);
        }

        void toolBarButtonPrint_Click(object sender, EventArgs e)
        {
             if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            TicketPurchase user = (TicketPurchase)listViewRecords.SelectedItem.Tag;
            PrintReceiptForm pr = new PrintReceiptForm(user.PurchaseID, m_current_user);
            pr.ShowDialog();
        }

        void toolBarButtonView_Click(object sender, EventArgs e)
        {
            loadUserForm(EditMode.Load);
        }

        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            loadUserForm(EditMode.Edit);
        }

        void enableButton(bool enable)
        {
            toolBarButtonDelete.Enabled = enable && m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonPrint.Enabled = enable && m_current_user.RoleDetails(this.Name).ViewFlag;
            toolBarButtonEdit.Enabled = enable && m_current_user.RoleDetails(this.Name).UpdateFlag;
            toolBarButtonView.Enabled = enable && m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        private void setPermission()
        {
            toolBarButtonDelete.Enabled = m_current_user.RoleDetails(this.Name).DeleteFlag;
            toolBarButtonEdit.Enabled = m_current_user.RoleDetails(this.Name).UpdateFlag;
            toolBarButtonClear.Enabled = m_current_user.RoleDetails(this.Name).NewFlag;
            toolBarButtonView.Enabled = m_current_user.RoleDetails(this.Name).ViewFlag;
        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            LoadAllTicketPurchase();
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            MessageBox.Show("Are you sure to delete this record?", "Confirmation", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, new EventHandler(DeleteConfirmation));
        }

        void DeleteConfirmation(object sender, EventArgs e)
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    TicketPurchase user = (TicketPurchase)listViewRecords.SelectedItem.Tag;
                    if (user.PurchaseID > 0)
                    {
                        m_ticketPurchaseService.DeleteTicketPurchase(user);
                        MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                       // clearForm();
                        LoadAllTicketPurchase();
                        //setEditMode(EditMode.Deleted);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonClear_Click(object sender, EventArgs e)
        {
            TicketPurchaseForm frm = new TicketPurchaseForm(this.Name, new TicketPurchase(), m_current_user, m_mainForm, this, EditMode.Create);
            frm.ShowDialog();
        }

        public void LoadAllTicketPurchase()
        {
            listViewRecords.Items.Clear();
            IList users = m_ticketPurchaseService.LoadAllTicketPurchase();
            foreach (TicketPurchase user in users)
            {
                user.TravelAgent = m_TravelAgentService.GetTravelAgentByID(user.TravelAgent.TravelAgentID);                
                ListViewItem item = listViewRecords.Items.Add(user.TransactionDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(user.Code);
                item.SubItems.Add(user.TravelAgent.Name);
                item.SubItems.Add(user.Quantity.ToString());
                item.SubItems.Add(user.UnitPrice.ToString("F"));
                item.SubItems.Add(user.TotalAmount.ToString("F"));

                item.Tag = user;
            }
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            IList users = m_ticketPurchaseService.SearcTicketPurchaseByCodeName(textBoxSearch.Text);
            foreach (TicketPurchase user in users)
            {
                user.TravelAgent = m_TravelAgentService.GetTravelAgentByID(user.TravelAgent.TravelAgentID);
                ListViewItem item = listViewRecords.Items.Add(user.TransactionDate.ToString("dd-MMM-yyyy"));
                item.SubItems.Add(user.Code);
                item.SubItems.Add(user.TravelAgent.Name);
                item.SubItems.Add(user.Quantity.ToString());
                item.SubItems.Add(user.UnitPrice.ToString("F"));
                item.SubItems.Add(user.TotalAmount.ToString("F"));
                item.Tag = user;
            }
            if (users.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (users.Count == 0) 
            {
                ListViewItem item = listViewRecords.Items.Add("Not Found.");
                textBoxSearch.SelectAll(); 
            }
        }

        private void listViewRecords_DoubleClick(object sender, EventArgs e)
        {
            //if (toolBarButtonEdit.Enabled)
            //    loadUserForm(EditMode.Edit);
            //else
            loadUserForm(EditMode.Load);
        }

        private void loadUserForm( EditMode editmode )
        {
            if (listViewRecords.SelectedItem == null) return;
            if (listViewRecords.SelectedItem.Tag == null) return;
            TicketPurchase user = (TicketPurchase)listViewRecords.SelectedItem.Tag;
            TicketPurchaseForm frm = new TicketPurchaseForm(this.Name, user, m_current_user, m_mainForm, this, editmode);
            frm.ShowDialog();
        }

        private void textBox_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
        }


        private void textBox_TextChanged(object sender, EventArgs e)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonFind_Click(null, null);
            }
        }

        private void listViewRecords_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyCode == Keys.Enter)
            {
                listViewRecords_DoubleClick(objSender, null);
            }
        }

        private void textBoxSearch_GotFocus(object sender, EventArgs e)
        {
            textBoxSearch.SelectAll();
        }

        private void listViewRecords_SelectedIndexChanged(object sender, EventArgs e)
        {
            enableButton(listViewRecords.SelectedItem != null);            
            if (listViewRecords.SelectedItem != null)
                enableButton(listViewRecords.SelectedItem.Tag != null);
        }

    }
}